<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;
use Mews\Captcha\Captcha;

class AuthenticationController extends Controller
{
    // 显示登陆页面
    public function getLogin(Request $request){
        return view('admin/login');
    }

    // 验证输入
    public function postLogin(Request $request){
        // 账号：admin
        // 密码：admin123
    	// 判断是否已经认证了
    	if(!Auth::check()){
    		// 表单验证
	    	$validate = array(
	    		'username' => 'required|min:5',
	    		'password' => 'required|min:8|alpha_num',
	    		'verification' => 'required|size:4|captcha'
	    	);
	    	$messages = array(
	    		'required' => ':attribute 必填',
	    		'min' => ':attribute 必须大于 :min',
	    		'size' => ':attribute 位数必须是 :size',
	    		'alpha_num' => ':attribute 只能输入数字或字母',
	    		'captcha' => '验证码不正确'
	    	);
	    	$this->validate($request,$validate,$messages);
	    	// 验证
	    	if(!Auth::attempt(['name' => $request->username, 'password' => $request->password])){
	    		// 认证不成功
	    		return redirect('admin/login')->withErrors(['用户名不存在或者密码错误']);
	    	}
	    	
    	}
    	// 跳转到 admin/index
    	return redirect('admin/index');
    }
}
